﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace FileProtector
{
    public partial class frmSettings : Form
    {
        //Settings
        Libraries.SettingParams sp;

        public frmSettings()
        {
            InitializeComponent();
            //Settings
            sp = new Libraries.SettingParams(System.IO.Path.GetDirectoryName(Application.ExecutablePath));

            //Initialize values
            txtName.Text = sp.Name;
            txtPassword.Text = sp.Password;
            txtCPassword.Text = sp.Password;
            txtVaultDir.Text = sp.VaultDirectory;

            //Don't show exit button
            if (this.FirstTimeUser != true)
                btnExit.Visible = false;
        }

        //Save
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("Enter Your Name", "Invalid Parameter");
                return;
            }

            if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show("Enter Password", "Invalid Parameter");
                return;
            }

            if (txtPassword.Text != txtCPassword.Text)
            {
                MessageBox.Show("Passowrd & Confirm Password doesn't match", "Invalid Parameter");
                return;
            }

            if (string.IsNullOrEmpty(txtVaultDir.Text.Trim()))
            {
                MessageBox.Show("Enter the Vault Directory", "Invalid Parameter");
                return;
            }

           
            Libraries.SettingParams sp = new Libraries.SettingParams(System.IO.Path.GetDirectoryName(Application.ExecutablePath));

            //Create directory
            sp.CreateDirectory(txtVaultDir.Text);

            string str = txtName.Text + sp.strDelim + txtPassword.Text + sp.strDelim + txtVaultDir.Text;
            sp.SaveSettings(str);

            frmMain frm = new frmMain();
            frm.Show();
        }

        //Load
        private void frmSettings_Load(object sender, EventArgs e)
        {
            //Hide Active forms
            frmMain.ActiveForm.Hide();
            frmPassword.ActiveForm.Hide();
        }

        //Show folder browser
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult dlrs = fld.ShowDialog();
            if (dlrs == DialogResult.OK)
                txtVaultDir.Text = fld.SelectedPath;
        }

        //Exit
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
